"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useDebounce = exports.useGenerateUUID = exports.useFmtSize = exports.useMobile = exports.useDeepClone = exports.useCopyToClipboard = void 0;
exports.useObjParams = useObjParams;
function useObjParams(url) {
    var params = {};
    if (url.indexOf('?') !== -1) {
        var index = url.indexOf('?') + 1;
        var str = url.substr(index);
        var ary = str.split('&');
        for (var i = 0; i < ary.length; i++) {
            params[ary[i].split('=')[0]] = ary[i].split('=')[1];
        }
    }
    return params;
}
;
var useCopyToClipboard = function (str) {
    var container = document.createElement('input');
    container.setAttribute('type', 'text');
    container.value = str;
    container.setAttribute('style', 'opacity: 0');
    document.body.appendChild(container);
    container.select();
    if (document.execCommand('copy')) {
        document.execCommand('copy');
    }
    document.body.removeChild(container);
};
exports.useCopyToClipboard = useCopyToClipboard;
var useDeepClone = function (source, recursion) {
    if (recursion) {
        if (source === null || source === undefined || source === 0 || !isNaN(source)) {
            return source;
        }
        if (source.constructor === String || source.constructor === Boolean) {
            return source;
        }
        if (source.constructor === Date) {
            return new Date(source);
        }
        var target = source.constructor === Array ? [] : {};
        if (source.constructor === Object) {
            for (var key in source) {
                if (source.hasOwnProperty(key)) {
                    if (source[key].constructor === Object) {
                        target[key] = (0, exports.useDeepClone)(source[key], false);
                    }
                    else {
                        target[key] = source[key];
                    }
                }
            }
        }
        else if (source.constructor === Array) {
            for (var i = 0; i < source.length; i++) {
                target.push((0, exports.useDeepClone)(source[i], false));
            }
        }
        return target;
    }
    else {
        if (source.constructor !== Object) {
            return source;
        }
        return JSON.parse(JSON.stringify(source));
    }
};
exports.useDeepClone = useDeepClone;
var useMobile = function (navigator) {
    if (navigator === null || navigator === void 0 ? void 0 : navigator.userAgent) {
        return {
            isMobile: /iPhone|iPad|iPod|Android|HarmonyOS/i.test(navigator.userAgent),
            isIOS: /iPhone|iPad|iPod/i.test(navigator.userAgent),
            isAndroid: /Android/i.test(navigator.userAgent),
            isHarmonyOS: /HarmonyOS/i.test(navigator.userAgent),
        };
    }
    else {
        throw new Error("Not the right Navigator Object, Please check your params!");
    }
};
exports.useMobile = useMobile;
var useFmtSize = function (size, digit) {
    var num = 0, unit = 'B';
    if (size === undefined) {
        size = 0;
    }
    if (size >= 0 && size < 1024) {
        num = size;
        unit = 'B';
    }
    else if (size >= 1024 && size < 1024 * 1024) {
        num = size / 1024;
        unit = 'KB';
    }
    else if (size >= 1024 * 1024 && size < 1024 * 1024 * 1024) {
        num = size / (1024 * 1024);
        unit = 'MB';
    }
    else if (size >= 1024 * 1024 * 1024) {
        num = size / (1024 * 1024 * 1024);
        unit = 'G';
    }
    return "".concat(num.toFixed(!digit ? 2 : digit)).concat(unit);
};
exports.useFmtSize = useFmtSize;
var useGenerateUUID = function () {
    var timestamp = new Date().getTime();
    var randomPart = Math.random().toString(16).substring(2, 18);
    var uuid = timestamp.toString(16) + randomPart;
    return uuid;
};
exports.useGenerateUUID = useGenerateUUID;
var useDebounce = function (fn, wait) {
    var timeout = null;
    return function (input) {
        input.persist();
        if (timeout !== null)
            clearTimeout(timeout);
        timeout = setTimeout(fn, wait, input);
    };
};
exports.useDebounce = useDebounce;
